<?php
namespace Endpoints\CandyCalendar;

use Endpoints\Base\Endpoint;
use Endpoints\CandyContent\IndexContent;
use Endpoints\Tools\GetOssObject;
use Laravelladder\Core\Collections\BaseCollection;
use Models\Candy\Calendar;
use Models\Candy\Content;
use Symfony\Component\Console\Input\InputArgument;

class IndexCalendar extends Endpoint
{
    // 定义参数
    const ARGUMENT_MEDIA_TYPE  = 'media_type';
    const ARGUMENT_DATE_BEFORE = 'date_before';

    // 收参
    public function getArguments()
    {
        return [
            [static::ARGUMENT_PAGE,InputArgument::OPTIONAL,'页数',1],
            [static::ARGUMENT_MEDIA_TYPE,InputArgument::OPTIONAL,'媒体类型',1],
            [static::ARGUMENT_P_LIMIT,InputArgument::OPTIONAL,'获取多少数据','10'],
            [static::ARGUMENT_DATE_BEFORE,InputArgument::OPTIONAL,'之前日期',''],
        ];
    }

    /**
     * 实现逻辑
     * @return \Laravelladder\Core\Collections\BaseCollection
     */
    public function dryRun()
    {
        $page   = $this->argument(static::ARGUMENT_PAGE);
        $limit  = $this->argument(static::ARGUMENT_P_LIMIT);
        $mediaType = $this->argument(static::ARGUMENT_MEDIA_TYPE);
        $offset = $limit * (($page > 0 ? $page : 1) - 1);
        $dateBefore  = $this->argument(static::ARGUMENT_DATE_BEFORE);


        $sql = "SELECT
                    :select_columns
                FROM
                     hi_candy_calendar 
                WHERE
                    deleted_at IS NULL ";

        if($dateBefore) $sql .= "AND date <= :dateBefore ";

        $sql .= " :order_by :pagination ";


        $list = $this
            ->getUniRepo()
            ->sqlSelectListWithPagination(
                $sql,
                [
                    'dateBefore' => $dateBefore
                ],
                [
                    '*',
                ],
                $limit,
                $offset,
                ['date'=>'desc','id'=>'desc']
                );
        $list->each(function(&$item)use($mediaType){
            /**
             * @var $item \Laravelladder\Core\Models\UniModel
             */
            $media = IndexContent::getInstance()->setArguments([
                IndexContent::ARGUMENT_DATE_ID    => $item->{Calendar::DB_FIELD_ID},
                IndexContent::ARGUMENT_MEDIA_TYPE => $mediaType,
                IndexContent::ARGUMENT_PAGE    => 1,
                IndexContent::ARGUMENT_P_LIMIT => 50,
            ])->dryRun();
            $item->addFillable('media',$media);
            $item->addFillable('imagesList',$media->getFieldArray("obj_url"));
            // 计算日期,出生默认一天，因此比较填24号
            $diff=date_diff(date_create($item->{Calendar::DB_FIELD_DATE}),date_create("2019-12-24"));
            $showDate = (($diff->y) ? ($diff->y).'年' : '') .
                        (($diff->m) ? ($diff->m).'个月' : '') .
                        (($diff->d) ? ($diff->d).'天' : '');
            $item->addFillable('showDate',$showDate);
            if(!$media->toArray()){
                unset($item);
            }
        });
        return $list;
    }
}